package com.cardone.platform.common.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.common.dto.EntityExtendDto;
import com.cardone.platform.common.service.EntityExtendService;

/**
 * 实体扩展
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/common/entityExtend/")
@Controller
public class EntityExtendController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 实体扩展：删除
	 *
	 * @param deleteEntityExtend
	 *          实体扩展对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final EntityExtendDto deleteEntityExtend) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(EntityExtendService.class).deleteByIds(deleteEntityExtend);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityExtendController.log);
				}
			}
		};
	}

	/**
	 * 实体扩展：分页查询
	 *
	 * @param findEntityExtend
	 *          实体扩展对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final EntityExtendDto findEntityExtend, final Model model) {
		final EntityExtendDto entityExtend = ContextHolder.getBean(EntityExtendService.class).findById(EntityExtendDto.class, findEntityExtend);

		model.addAttribute("entityExtend", entityExtend);
	}

	/**
	 * 实体扩展：编辑
	 *
	 * @param findEntityExtend
	 *          实体扩展对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<EntityExtendDto>> findByIdJson(final EntityExtendDto findEntityExtend, final Model model) {
		return new Callable<ReturnDataDto<EntityExtendDto>>() {
			@Override
			public ReturnDataDto<EntityExtendDto> call() throws Exception {
				try {
					final EntityExtendDto entityExtend = ContextHolder.getBean(EntityExtendService.class).findById(EntityExtendDto.class, findEntityExtend);

					return ReturnDataUtils.newReturnData(entityExtend);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityExtendController.log);
				}
			}
		};
	}

	/**
	 * 实体扩展：查询
	 *
	 * @param findListEntityExtend
	 *          实体扩展对象
	 * @return 实体扩展对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<EntityExtendDto>>> findListByLikeCodeJson(final EntityExtendDto findListEntityExtend) {
		return new Callable<ReturnDataDto<List<EntityExtendDto>>>() {
			@Override
			public ReturnDataDto<List<EntityExtendDto>> call() throws Exception {
				try {
					final List<EntityExtendDto> entityExtendList = ContextHolder.getBean(EntityExtendService.class).findListByLikeCode(EntityExtendDto.class, findListEntityExtend);

					return ReturnDataUtils.newReturnData(entityExtendList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityExtendController.log);
				}
			}
		};
	}

	/**
	 * 实体扩展：分页查询
	 *
	 * @param paginationEntityExtend
	 *          实体扩展对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<EntityExtendDto>>> paginationByLikeCodeJson(final EntityExtendDto paginationEntityExtend) {
		return new Callable<ReturnDataDto<PaginationDto<EntityExtendDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<EntityExtendDto>> call() throws Exception {
				try {
					final PaginationDto<EntityExtendDto> entityExtendPagination = ContextHolder.getBean(EntityExtendService.class).paginationByLikeCode(EntityExtendDto.class, paginationEntityExtend);

					return ReturnDataUtils.newReturnData(entityExtendPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityExtendController.log);
				}
			}
		};
	}

	/**
	 * 实体扩展：分页查询
	 *
	 * @param paginationEntityExtend
	 *          实体扩展对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final EntityExtendDto paginationEntityExtend, final Model model) {
		final PaginationDto<EntityExtendDto> entityExtendPagination = ContextHolder.getBean(EntityExtendService.class).paginationByLikeCode(EntityExtendDto.class, paginationEntityExtend);

		model.addAttribute("entityExtendPagination", entityExtendPagination);
	}

	/**
	 * 实体扩展：保存
	 *
	 * @param saveEntityExtend
	 *          实体扩展对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final EntityExtendDto saveEntityExtend) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(EntityExtendService.class).saveByIdOrCode(EntityExtendDto.class, saveEntityExtend);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityExtendController.log);
				}
			}
		};
	}
}
